let ask () = 
    let text = GToolbox.input_string ~title:"Shell"
      ~ok:"Ok" ~cancel:"Annuler" "Command ?" in
    match text with
    | None -> ""
    | Some x -> x

let rec print_results = function
  | [] -> ()
  | h::t -> print_endline h; print_results t

let execute_command command b (c : GSourceView2.source_view) (d : GSourceView2.source_view) read writeme = 
    if not(b) then
        print_results (Compil.read_process_lines command c)
    else
        Compil.send_shell command d read writeme

let svn arg arg2 func b (c : GSourceView2.source_view) d (read : Unix.file_descr) (writeme : out_channel) = match func with
  | "add" -> execute_command ("svn add "^arg) b c d read writeme
  | "checkout" -> execute_command ("svn checkout "^arg) b c d read writeme
  | "checkout-redo" -> execute_command ("svn checkout -r "^arg^" "^arg2) b c d read writeme
  | "cleanup" -> execute_command ("svn cleanup") b c d read writeme
  | "commit" -> execute_command ("svn commit -m '"^arg^"'") b c d read writeme
  | "commit-file" -> execute_command ("git commit -m '"^arg^"' "^arg) b c d read writeme
  | "diff" -> execute_command ("svn diff "^arg) b c d read writeme
  | "diff-rev" -> execute_command ("svn diff -r "^arg^" "^arg2) b c d read writeme
  | "export" -> execute_command ("svn export "^arg) b c d read writeme
  | "export-rev" -> execute_command ("svn export -r "^arg^" "^arg2) b c d read writeme
  | "info" -> execute_command ("svn info "^arg) b c d read writeme
  | "list" -> execute_command ("svn list") b c d read writeme
  | "list-rev" -> execute_command ("svn list -r "^arg^" "^arg2) b c d read writeme
  | "log" -> execute_command ("svn log "^arg) b c d read writeme
  | "mkdir" -> execute_command ("svn mkdir "^arg) b c d read writeme
  | "remove" -> execute_command ("svn remove "^arg) b c d read writeme
  | "resolved" -> execute_command ("svn resolved "^arg) b c d read writeme
  | "revert" -> execute_command ("svn revert") b c d read writeme
  | "switch" -> execute_command ("svn switch "^arg) b c d read writeme
  | "status" -> execute_command ("svn status") b c d read writeme
  | "status-update" -> execute_command ("svn status -u") b c d read writeme
  | "undo" -> execute_command ("svn merge -r "^arg^" "^arg2) b c d read writeme
  | "update" -> execute_command ("svn update") b c d read writeme
  | _ -> ()

let git arg arg2 func b (c : GSourceView2.source_view) d read writeme = match func with
  | "add" -> execute_command ("git add "^arg) b c d read writeme
  | "branch" -> execute_command ("git branch "^arg) b c d read writeme
  | "checkout" -> execute_command ("git checkout "^arg) b c d read writeme
  | "checkout-new" -> execute_command ("git checkout -b "^arg) b c d read writeme
  | "checkout-redo" -> execute_command ("git checkout -- "^arg) b c d read writeme
  | "checkout-rm" -> execute_command ("git checkout -d "^arg) b c d read writeme
  | "clone" -> execute_command ("git clone "^arg) b c d read writeme
  | "commit" -> execute_command ("git commit -m '"^arg^"'") b c d read writeme
  | "commit-file" -> execute_command ("git commit -m '"^arg^"' "^arg) b c d read writeme
  | "diff" -> execute_command ("git diff "^arg^" "^arg2) b c d read writeme
  | "fetch" -> execute_command "git fetch origin" b c d read writeme
  | "history" -> execute_command "git log --style compact" b c d read writeme
  | "merge" -> execute_command ("git merge "^arg) b c d read writeme
  | "pull" -> execute_command "git pull" b c d read writeme
  | "push" -> execute_command ("git push origin "^arg) b c d read writeme
  | "rm" -> execute_command ("hg rm "^arg) b c d read writeme
  | "tag" -> execute_command ("git tag "^arg^" "^arg2) b c d read writeme
  | _ -> ()

let hg arg func b (c : GSourceView2.source_view) d read writeme = match func with
  | "add" -> execute_command ("hg add "^arg) b c d read writeme
  | "branch" -> execute_command ("hg branch "^arg) b c d read writeme
  | "branches" -> execute_command "hg branches" b c d read writeme
  | "clone" -> execute_command ("hg clone "^arg) b c d read writeme
  | "commit" -> execute_command ("hg commit -m '"^arg^"'") b c d read writeme
  | "commit-file" -> execute_command ("hg commit -m '"^arg^"' "^arg) b c d read writeme
  | "diff" -> execute_command "hg diff" b c d read writeme
  | "diff-file" -> execute_command ("hg diff "^arg) b c d read writeme
  | "heads" -> execute_command "hg heads --style compact" b c d read writeme
  | "history" -> execute_command "hg log --style compact" b c d read writeme
  | "incoming" -> execute_command "hg incoming" b c d read writeme
  | "merge" -> execute_command ("hg merge "^arg) b c d read writeme
  | "pull" -> execute_command "hg pull" b c d read writeme
  | "push" -> execute_command "hg push" b c d read writeme
  | "remove" -> execute_command ("hg remove "^arg) b c d read writeme
  | "status" -> execute_command "hg status" b c d read writeme
  | "update" -> execute_command "hg update" b c d read writeme
  | "update-redo" -> execute_command ("hg update -C "^arg) b c d read writeme
  | "update-branch" -> execute_command ("hg update "^arg) b c d read writeme
  | _ -> () 

